<template>
  <ul class="list">
    <li
      class="item"
      :ref="item"
      @touchstart.prevent="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
      @click="letterClick"
      v-for="item in letters"
      :key="item"
    >{{item}}</li>
  </ul>
</template>
<script>
export default {
  data() {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    };
  },
  props: {
    cities: Object
  },
  computed: {
    letters() {
      const letters = [];
      for (let i in this.cities) {
        letters.push(i);
      }
      return letters;
    }
  },
  updated() {
    this.startY = this.$refs["A"][0].offsetTop;
  },
  methods: {
    letterClick(e) {
      this.$emit("changeLetter", e.target.innerText);
    },
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      if (this.timer) {
        clearTimeout(this.timer);
      }
      setTimeout(() => {
        //获取当前的手指的高度
        const touchY = e.touches[0].clientY - 79;

        //获取到索引下标
        let index = Math.floor((touchY - this.startY) / 20);
        if (index >= 0 && index < this.letters.length) {
          this.$emit("changeLetter", this.letters[index]);
        }
      }, 16);
    },
    handleTouchEnd() {
      this.touchStatus = false;
    }
  },
  name: "alphabet"
};
</script>
<style lang="stylus" scoped>
@import '~@/assets/stylus/var.stylu';

.list {
  position: absolute;
  right: 0;
  top: 1.58rem;
  bottom: 0;
  width: 0.4rem;
  display: flex;
  flex-direction: column;
  justify-content: center;

  .item {
    color: $bgColor;
    line-height: 0.4rem;
    text-align: center;
  }
}
</style>